import 'package:azlistview/azlistview.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:personinfo/model/home/CarBrandModel.dart';
import 'package:personinfo/model/home/SearchCarModel.dart';
import 'package:personinfo/utils/ColorUtil.dart';
import 'package:personinfo/widgets/common/CommonWidget.dart';

import '../../../utils/commonUtil.dart';


class SelectCarBrand extends StatefulWidget {
  SearchCarModel arguments;
  SelectCarBrand({super.key,  required this.arguments});
  @override
  _SelectCarBrandState  createState() => _SelectCarBrandState();
}

List<CarBrandModel> _generateCarBrandModels() {
  final data = <CarBrandModel>[
    CarBrandModel('A')..suspensionTag = 'A',
    CarBrandModel('奥迪')..suspensionTag = 'A',
    CarBrandModel('B')..suspensionTag = 'B',
    CarBrandModel('宝马')..suspensionTag = 'B',
    CarBrandModel('奔驰')..suspensionTag = 'B',
    CarBrandModel('比亚迪')..suspensionTag = 'B',
    CarBrandModel('C')..suspensionTag = 'C',
    CarBrandModel('长安')..suspensionTag = 'C',
    CarBrandModel('D')..suspensionTag = 'D',
    CarBrandModel('大众')..suspensionTag = 'D',
    CarBrandModel('F')..suspensionTag = 'F',
    CarBrandModel('丰田')..suspensionTag = 'F',
    CarBrandModel('福特')..suspensionTag = 'F',
    CarBrandModel('J')..suspensionTag = 'J',
    CarBrandModel('吉利')..suspensionTag = 'J',
    CarBrandModel('本田')..suspensionTag = 'B',
    CarBrandModel('X')..suspensionTag = 'X',
    CarBrandModel('现代')..suspensionTag = 'X',
    CarBrandModel('雪佛兰')..suspensionTag = 'X',
    CarBrandModel('W')..suspensionTag = 'W',
    CarBrandModel('蔚来')..suspensionTag = 'W',
    CarBrandModel('小鹏')..suspensionTag = 'X',
    CarBrandModel('L')..suspensionTag = 'L',
    CarBrandModel('理想')..suspensionTag = 'L',
  ];

  // 按 suspensionTag 排序（A-Z）
  data.sort((a, b) => a.suspensionTag!.compareTo(b.suspensionTag!));
  return data;
}

class _SelectCarBrandState extends State<SelectCarBrand>  {
  List<CarBrandModel>  brands=  _generateCarBrandModels();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Theme.of(context).colorScheme.onPrimary,
      appBar: commonWidget.appBar(
        title: const Text("选择品牌"),
        leading: GestureDetector(
          onTap: () {
            Navigator.pop(context);
          },
          child: const Icon(Icons.keyboard_arrow_left),
        ),
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(62),
          child: commonUtil.searchBorder(context,onTap: (){
            Navigator.pushNamed(context, "/carResourceSearch");
          }),
        ),
      ),
      body: AzListView(
          data: brands,
          itemCount: brands.length,
          indexBarOptions: IndexBarOptions(
            textStyle: TextStyle(fontSize: 15),
          ),
          itemBuilder: (BuildContext context, int index) {
            final brand = brands[index];
            // 判断是否为字母类型（与suspensionTag相同）
            bool isLetterItem = brand.name == brand.suspensionTag;

            if (isLetterItem) {
              return Container(
                height: 40,
                width: MediaQuery.of(context).size.width,
                color: Colors.grey[200],
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(left: 16),
                child: Text(
                  brand.suspensionTag!,
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
              );
            } else {
              return Container(
                padding: EdgeInsets.zero,
                decoration: BoxDecoration(
                  color: widget.arguments.carBrand == brand.name ? ColorUtil.primaryBadgeBgColor : null, // 设置背景色
                  border: Border(
                    bottom: BorderSide(
                      color: Colors.grey[300]!,
                      width: 0.5,
                    ),
                  ),
                ),
                height: 48,
                child: ListTile(
                  contentPadding: EdgeInsets.symmetric(horizontal: 20, vertical: 2),
                  leading: Icon(Icons.directions_car),
                  title: Text(brand.name, style: TextStyle(color: widget.arguments.carBrand == brand.name  ? Colors.white : Colors.black)),
                  onTap: () {
                    setState(() {
                      widget.arguments.carBrand = brand.name;
                    });
                    // 跳转到车系
                    Navigator.pushNamed(context, "/selectCarSeries", arguments: widget.arguments);
                  },
                  mouseCursor: SystemMouseCursors.click,
                ),
              );
            }

          },
        // 粘性头部（如 "A", "B"）
        susItemBuilder: (BuildContext context, int section) {
          return Container(
            height: 40,
            width: MediaQuery.of(context).size.width,
            color: Colors.grey[200],
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.only(left: 16),
            child: Text(
              brands[section].suspensionTag!,
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
          );
        },
          indexBarData: brands.map((e) => e.suspensionTag!).toSet().toList()
            ..sort()
      ),
    );
  }
}
